package com.teachoa.web;

import com.jfinal.plugin.activerecord.Page;
import com.teachoa.bean.TeacherStudent;
import com.teachoa.bean.User;
import com.teachoa.enumtype.UserTypeEnum;

public class TeachController extends BaseController {
	
	public void index() {
		int pageNo = getParaToInt("pageNo", 1);
		Page<User> teacherPage = User.dao.getUserPageByType(pageNo, UserTypeEnum.TEACHER.getCode());
		//System.err.println(teacherPage.getList().size());
		setAttr("teacherPage", teacherPage);
		
		Page<User> studentPage = User.dao.getUserPageByType(pageNo, UserTypeEnum.STUDENT.getCode());
		setAttr("studentPage", studentPage);
		renderJsp("teach.jsp");
	}
	
	public void loadStudents() {
		String teacherId = getPara();
		int pageNo = getParaToInt("pageNo", 1);
		Page<User> studentPage = User.dao.getStudentsByTeacherId(pageNo, teacherId);
		setAttr("studentPage", studentPage);
		renderJsp("student_table.jsp");
	}
	
	public void deleteRelation() {
		String studentId = getPara(0);
		String teacherId = getPara(1);
		try {
			TeacherStudent.dao.deleteByStudentIdAndTeacherId(studentId, teacherId);
			renderJson(success("删除成功！！"));
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(fail("删除失败！！"));
		}
		
	}

}
